package com.team8.auth.service.service;

import com.team8.auth.common.pojo.UserInfo;
import com.team8.auth.common.utils.JwtUtils;
import com.team8.auth.service.client.UserClient;
import com.team8.auth.service.config.JwtProperties;
import com.team8.common.utils.NumberUtils;
import com.team8.user.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@EnableConfigurationProperties(JwtProperties.class)
public class AuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties jwtProperties;

    public String authentication(String email, String pwd) {
        User user = userClient.queryUser(email, pwd);

        if(user == null){
            return null;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setId((long)user.getUserId());
        userInfo.setUsername(user.getEmail());
        try {
            return JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
